<?php
class Modelteam extends CI_Model {
    /**
	 * Function __construct().
	 * used for Initialize ModelMaterial models
	 */
	
	function __construct()
    {
        parent::__construct();
    }

    /*
     * Function used For Listing
    * param $config string passing configaration datas
    * param $start  string initializing start of listing
    * param $param  string,passing parameters
    * purpose to fetch data for listing
    */
    
	function getList(&$config,&$start,&$param)
	{

		// GET DATA FROM GET/POST  OR   SESSION ====================
		$Count = 0;
		$page = $this->uri->segment(3,0); // page
		$isSession = $this->uri->segment(4,0); // read data from SESSION or POST     (1 == POST , 0 = SESSION )

		$start = 0;

		$sortType 		= $param['sortType'];
		$sortField 		= $param['sortField'];
		$searchField 	= $param['searchField'];
		$searchString 	= $param['searchString'];
		$searchText 	= $param['searchText'];
		$searchDisplay 	= $param['searchDisplay'];

		if($isSession == 0)
		{
			$sortType    	= $this->nsession->get_param('ADMIN_TEAM','sortType','ASC');
			$sortField   	= $this->nsession->get_param('ADMIN_TEAM','sortField','team_name');
                       // $sortField   	= $this->nsession->get_param('ADMIN_TEAM','sortField','team_short_name');
			$searchField 	= $this->nsession->get_param('ADMIN_TEAM','searchField','');
			$searchString 	= $this->nsession->get_param('ADMIN_TEAM','searchString','');
			$searchText  	= $this->nsession->get_param('ADMIN_TEAM','searchText','');
			$searchDisplay  = $this->nsession->get_param('ADMIN_TEAM','searchDisplay',20);
		}

		//========= SET SESSION DATA FOR SEARCH / PAGE / SORT Condition etc =====================
		$sessionDataArray = array();
		$sessionDataArray['sortType'] 		= $sortType;
		$sessionDataArray['sortField'] 		= $sortField;
		$sessionDataArray['searchField'] 	= $searchField;
		$sessionDataArray['searchString'] 	= $searchString;
		$sessionDataArray['searchText'] 	= $searchText;
		$sessionDataArray['searchDisplay'] 	= $searchDisplay;

		$this->nsession->set_userdata('ADMIN_TEAM', $sessionDataArray);
		//==============================================================

		$orderBy = " order by ".$sortField." ".$sortType;
		//$where = " WHERE is_active ='Y' " ;
		$where = '';

		if($searchField == "")
		{
		  $searchField='team_name';
		}

		if($searchString <> "" )
		{
		 	$where .= " where ".$searchField. " like '%".$searchString."%' ";
		}

		$sql="SELECT COUNT(id) as TotalrecordCount FROM ".TEAM.$where;
		$recordSet = $this->db->query($sql);

		$config['total_rows'] = 0;
		$config['per_page'] = $searchDisplay;
		if ($recordSet)
		{
         	$row = $recordSet->row();
	   		$config['total_rows'] = $row->TotalrecordCount;
		}
		else
		{
			return false;
		}

		if($page > 0 && $page < $config['total_rows'] )
			$start = $page;

		 $sql=" SELECT * FROM ".TEAM." $where $orderBy  LIMIT ".$start.",".$config['per_page'] ;
		    
		$recordSet = $this->db->query($sql);

		$rs = false;
		if ($recordSet->num_rows() > 0)
        {
           	$rs = array();
			$isEscapeArr = array();
			foreach ($recordSet->result_array() as $row)
				{
					foreach($row as $key=>$val){
						if(!in_array($key,$isEscapeArr)){
							$recordSet->fields[$key] = outputEscapeString($val);
						}
					}
					$rs[] = $recordSet->fields;
				}
        }
		else
		{
			return false;
		}
		return $rs;
	}

	/*
	 * Function used For Delete
	* param $id string passing id
	* purpose to delete data
	*/
	
	function delete($id)
	{
	   
		$sql = "DELETE FROM ".TEAM." WHERE id = ".$id."";
		$recordSet = $this->db->query($sql);					
	    
	}
	
	
	function addTeam($data)
	{
		//pr($data);
				$sql = "INSERT INTO ".TEAM."  SET
				   game_id                                = '".$data['game_id']."',
				team_name			= '".$data['team_name']."',
                                    
                                team_short_name   = '".$data['team_short_name']."' ,
                                xml_team_id  =  '".$data['xml_team_id']."' ";
                                
				
		
		$result = $this->db->query($sql);
		
		/*$result = $this->db->insert(TEAM, $data);*/
		$last_insert_id = $this->db->insert_id();

		if($result)
		{
			return $last_insert_id;
		}
		else
		{
			return false;
		}
	}

	/*
	 * Function used For Edit
	* param $data Array passing Post Values
	* param $id string passing id
	*/
	
	function editTeam($id,$data)
	{
		$sql = "UPDATE ".TEAM."  SET
				team_name				= '".$data['team_name']."',
				team_short_name                         = '".$data['team_short_name']."',
                                game_id                                = '".$data['game_id']."',
                                xml_team_id                            = '".$data['xml_team_id']."'
				WHERE id 			= ".$id."";
                                
	
		$result = $this->db->query($sql);
		/*$this->db->where("id", $id);
		$result = $this->db->update(TEAM, $data);*/
		$affected_rows =$this->db->affected_rows();

		if($result)
		{
			return $affected_rows;
		}
		else
		{
			return false;
		}
	}

	function getSingle($id)
	{
		$sql = "SELECT * FROM ".TEAM." WHERE id = ".$id;
		$recordSet = $this->db->query($sql);

		$rs = false;
		if ($recordSet->num_rows() > 0)
        {
           	$rs = array();
			
			foreach ($recordSet->result_array() as $row)
				{
					foreach($row as $key=>$val){
						
							$recordSet->fields[$key] = outputEscapeString($val);
						
					}
					$rs[] = $recordSet->fields;
				}
        }
		return $rs;
	}
	function mapTeamFormat($formatid,$id){
            
            for($i=0; $i<count($formatid) ; $i++)
            {
                $sql = "INSERT INTO ".MPTEAMGAMEFORMAT."SET
                    team_id = '".$id."',
                        game_format_id = '".$formatid[$i]."' "; 
                $this->db->query($sql);
            }
            
            return ;
                 
        }
        function deletemapTeamFormat($id){
            
                $sql = "DELETE FROM ".MPTEAMGAMEFORMAT." WHERE
                    team_id = '".$id."' ";
                $this->db->query($sql);
                return TRUE;
        }
}

?>